package com.jxy.filesystem.service;

import com.alibaba.fastjson.JSON;
import com.jxy.edu.common.exception.ExceptionCast;
import com.jxy.edu.common.model.response.CommonCode;
import com.jxy.edu.domain.filesystem.FileSystem;
import com.jxy.edu.domain.filesystem.response.FileSystemCode;
import com.jxy.edu.domain.filesystem.response.UploadFileResult;
import com.jxy.filesystem.dao.FileSystemRepository;
import org.apache.commons.lang3.StringUtils;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author lumi
 * @date 2020/2/13 19:28
 */
@Service
public class FileSystemService {
    @Autowired
    FileSystemRepository fileSystemRepository;


    @Value("${xuecheng.fastdfs.tracker_servers}")
    String tracker_servers;
    @Value("${xuecheng.fastdfs.connect_timeout_in_seconds}")
    int connect_timeout_in_seconds;
    @Value("${xuecheng.fastdfs.network_timeout_in_seconds}")
    int network_timeout_in_seconds;
    @Value("${xuecheng.fastdfs.charset}")
    String charset;

    /**
     *  上传文件
     * @param multipartFile
     * @param filetag
     * @param businesskey
     * @param metadata
     * @return
     */
    public UploadFileResult upload(MultipartFile multipartFile, String filetag, String businesskey, String metadata) {
        if(multipartFile ==null){
            ExceptionCast.cast(FileSystemCode.FS_UPLOADFILE_FILEISNULL);
        }

        //第一步：将文件上传到fastDFS中，得到一个文件id
        String fileId = fdfs_upload(multipartFile);
        if(StringUtils.isEmpty(fileId)){
            ExceptionCast.cast(FileSystemCode.FS_UPLOADFILE_SERVERFAIL);
        }
        //第二步：将文件id及其它文件信息存储到mongodb中。
        FileSystem fileSystem = new FileSystem();
        fileSystem.setFileId(fileId);
        fileSystem.setFilePath(fileId);
        fileSystem.setFiletag(filetag);
        fileSystem.setBusinesskey(businesskey);
        fileSystem.setFileName(multipartFile.getOriginalFilename());
        fileSystem.setFileType(multipartFile.getContentType());
        if(StringUtils.isNotEmpty(metadata)){
            try {
                Map map = JSON.parseObject(metadata, Map.class);
                fileSystem.setMetadata(map);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        fileSystemRepository.save(fileSystem);
        System.out.println(fileSystem);
        return new UploadFileResult(CommonCode.SUCCESS,fileSystem);
    }
   private String fdfs_upload(MultipartFile multipartFile){
//初始化
       initFdfsConfig();
       //创建trackerClient
       TrackerClient trackerClient = new TrackerClient();

       try {
           TrackerServer trackerServer = trackerClient.getConnection();
           //得到storage服务器
           StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);
           //创建storageClient来上传文件
           StorageClient1 storageClient1 = new StorageClient1(trackerServer,storeStorage);
           //上传文件
           //得到文件字节
           byte[] bytes = multipartFile.getBytes();
           //得到文件的原始名称
           String originalFilename = multipartFile.getOriginalFilename();
           //得到文件扩展名
           String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
           String fileId = storageClient1.upload_file1(bytes, ext, null);
           return fileId;
       } catch (Exception e) {
           e.printStackTrace();
       }
       return null;
   }
    //初始化fastDFS环境
    private void initFdfsConfig(){
        //初始化tracker服务地址（多个tracker中间以半角逗号分隔）
        try {
            ClientGlobal.initByTrackers(tracker_servers);
            ClientGlobal.setG_charset(charset);
            ClientGlobal.setG_network_timeout(network_timeout_in_seconds);
            ClientGlobal.setG_connect_timeout(connect_timeout_in_seconds);
        } catch (Exception e) {
            e.printStackTrace();
            //抛出异常
            ExceptionCast.cast(FileSystemCode.FS_INITFDFSERROR);
        }
    }
}
